//
//  HTWaybillDetialViewController.m
//  TruckDriver
//
//  Created by Ting Han on 2017/9/28.
//  Copyright © 2017年 Ting Han. All rights reserved.
//

#import "HTWaybillDetialViewController.h"
#import "HTWaybillStateCell.h"
#import "HTWaybillStateHeadCell.h"
#import "HTWaybillFooterCell.h"
#import "HTCommentView.h"

@interface HTWaybillDetialViewController ()<UITableViewDelegate,UITableViewDataSource,HTCommentViewDelegate>
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomHeight;
@property (weak, nonatomic) IBOutlet UIButton *normalContactButton;
@property (weak, nonatomic) IBOutlet UIButton *CommentContactButton;
@property (weak, nonatomic) IBOutlet UIButton *commentButton;
@property (weak, nonatomic) IBOutlet UIView *bottomView;

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic,strong) HTWaybillDetialModel *waybillDetialModel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *safeBottomHeight;

@end

@implementation HTWaybillDetialViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"运单详情";
    self.safeBottomHeight.constant = IPHONE_SAFEBOTTOMAREA_HEIGHT; self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"HTWaybillStateCell" bundle:nil] forCellReuseIdentifier:WaybillStateCell];
     [self.tableView registerNib:[UINib nibWithNibName:@"HTWaybillFooterCell" bundle:nil] forCellReuseIdentifier:WaybillFooterCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"HTWaybillStateHeadCell" bundle:nil] forCellReuseIdentifier:WaybillStateHeadCell];
    if ([self.waybillModel.state integerValue] == 0 || [self.waybillModel.state integerValue] == 1) {
       //未接单，运输中
        self.commentButton.hidden = YES;
        self.CommentContactButton.hidden = YES;
        
    }
    if ([self.waybillModel.state integerValue] == 2) {
        //待评价
        self.normalContactButton.hidden = YES;
        
    }
    if ([self.waybillModel.state integerValue] == 3 || [self.waybillModel.state integerValue] == 4) {
      //完成、取消
       self.bottomHeight.constant = 0;
       self.bottomView.hidden = YES;
    }
    [self netLoadWaybillDetial];
   
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.waybillDetialModel == nil) {
        return 1;
    }else if ([self.waybillDetialModel.state integerValue] == 3){
        return 3;
    }else{
        return 2;
    }
   
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row == 0){
        if (self.waybillDetialModel == nil) {
            if ([self.waybillModel.state integerValue] == 4) {
                return 40;
            }
        }else{
            if ([self.waybillDetialModel.state integerValue] == 4) {
                return 40;
            }
        }
        return 95;
    }else if(indexPath.row == 1){
        return 375;
    }else{
        return 100;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row == 0){
        HTWaybillStateHeadCell *cell = [tableView dequeueReusableCellWithIdentifier:WaybillStateHeadCell forIndexPath:indexPath];
        NSString *state = @"";
        if (self.waybillDetialModel == nil) {
            state = self.waybillModel.state;
        }else{
            state = self.waybillDetialModel.state;
        }
        if ([state integerValue] == 0) {
            cell.waybillStateType = HTWaybillStateTypeFirst;
        }
        if ([state integerValue] == 1) {
             cell.waybillStateType = HTWaybillStateTypeSecond;
        }
        if ([state integerValue] == 2) {
             cell.waybillStateType = HTWaybillStateTypeThird;
        }
        if ([state integerValue] == 3) {
             cell.waybillStateType = HTWaybillStateTypeForth;
        }
        if ([state integerValue] == 4) {
             cell.waybillStateType = HTWaybillStateTypeFifth;
        }
        return cell;
    }else if(indexPath.row == 1){
        HTWaybillStateCell *cell = [tableView dequeueReusableCellWithIdentifier:WaybillStateCell forIndexPath:indexPath];
        if (self.waybillDetialModel) {
            cell.waybillDetialModel = self.waybillDetialModel;
        }
        return cell;
    }else{
        HTWaybillFooterCell *cell = [tableView dequeueReusableCellWithIdentifier:WaybillFooterCell forIndexPath:indexPath];
        cell.impressString = self.waybillDetialModel.impress;
        cell.scrollString = self.waybillDetialModel.scorell;
        [cell setupSubView];
        return cell;
    }
}
- (void)setWaybillDetialModel:(HTWaybillDetialModel *)waybillDetialModel{
    _waybillDetialModel = waybillDetialModel;
    if ([waybillDetialModel.state integerValue] == 0 || [waybillDetialModel.state integerValue] == 1) {
        //未接单，运输中
        self.commentButton.hidden = YES;
        self.CommentContactButton.hidden = YES;
        
    }
    if ([waybillDetialModel.state integerValue] == 2) {
        //待评价
        self.normalContactButton.hidden = YES;
    }
    if ([waybillDetialModel.state integerValue] == 3 || [waybillDetialModel.state integerValue] == 4) {
        //完成、取消
        self.bottomHeight.constant = 0;
        self.bottomView.hidden = YES;
    }
}
#pragma mark --- 点击事件
- (IBAction)contactAction:(id)sender {
    if (self.waybillDetialModel == nil) {
        [HTShowUtil showMessage:@"运单详情获取失败"];
        return;
    }
    NSString *phone = self.waybillDetialModel.phones;
    NSString *str=[[NSMutableString alloc] initWithFormat:@"tel:%@",phone];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}
- (IBAction)commentAction:(id)sender {
    if (self.waybillDetialModel == nil ) {
        [HTShowUtil showMessage:@"信息加载失败"];
        return;
    }
    HTCommentView *commentView = [[HTCommentView alloc]init];
    commentView.orderid = self.waybillDetialModel.orderid;
    commentView.delegate = self;
    [commentView showView];
}
//评价货主
- (void)didCommitComment:(NSString *)message scroll:(NSString *)scroll orderid:(NSString *)orderid commentView:(HTCommentView *)commentView{
    if (orderid == nil || orderid.length <= 0) {
        return;
    }
    [self startHud];
    [HTHttpRequestManager requestWayBillCommentWithOrderid:orderid scorll:scroll impress:message success:^(id  _Nullable responseObject) {
        [self endHud];
        DBLog(@"提交评论：%@",responseObject);
        if (!responseObject) {
            [HTShowUtil showMessage:@"评价失败"];
            return ;
        }
        NSString *codeStr = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
        if (![codeStr isEqualToString:@"200"]) {
            [HTShowUtil showMessage:responseObject[@"message"]];
            return;
        }
        [HTShowUtil showMessage:responseObject[@"message"]];
        [commentView dismissView];
        [self netLoadWaybillDetial];
        
    } failure:^(NSError * _Nonnull error) {
        [self endHud];
        [HTShowUtil showMessage:@"评价失败"];
    }];
}

#pragma mark --- 网络请求
- (void)netLoadWaybillDetial{
    [self startHud];
    [HTHttpRequestManager requestWayBillDetialWithOrderid:self.waybillModel.orderid success:^(id  _Nullable responseObject) {
        [self endHud];
        DBLog(@"运单详情：%@",responseObject);
        if (!responseObject) {
            [HTShowUtil showMessage:@"获取运单详情失败"];
            return ;
        }
        NSString *codeStr = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
        if (![codeStr isEqualToString:@"200"]) {
            [HTShowUtil showMessage:responseObject[@"message"]];
            return;
        }
        self.waybillDetialModel = [HTWaybillDetialModel mj_objectWithKeyValues:responseObject[@"data"]];
        [self.tableView reloadData];
    } failure:^(NSError * _Nonnull error) {
        [self endHud];
         [HTShowUtil showMessage:@"获取运单详情失败"];
    }];
}
@end


